package cn.yuxiaoyuan.common.util;

import java.security.SecureRandom;
import java.util.Random;

/**
 * 随机字符串生成工具类
 */
public class RandomStringUtils {

    private static final String UPPER = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    private static final String LOWER = "abcdefghijklmnopqrstuvwxyz";
    private static final String DIGITS = "0123456789";
    private static final String SPECIAL = "!@#$%^&*()-_=+[]{}|;:,.<>?";
    private static final String ALPHA = UPPER + LOWER;
    private static final String ALPHA_NUMERIC = ALPHA + DIGITS;
    private static final String ALL_CHARS = ALPHA_NUMERIC + SPECIAL;
    
    private static final Random RANDOM = new SecureRandom();

    /**
     * 生成指定长度的纯字母随机字符串
     * @param length 字符串长度
     * @return 随机字符串
     */
    public static String randomAlphabetic(int length) {
        return random(length, ALPHA);
    }

    /**
     * 生成指定长度的字母数字随机字符串
     * @param length 字符串长度
     * @return 随机字符串
     */
    public static String randomAlphanumeric(int length) {
        return random(length, ALPHA_NUMERIC);
    }

    /**
     * 生成指定长度的数字随机字符串
     * @param length 字符串长度
     * @return 随机字符串
     */
    public static String randomNumeric(int length) {
        return random(length, DIGITS);
    }

    /**
     * 生成包含特殊字符的随机字符串
     * @param length 字符串长度
     * @return 随机字符串
     */
    public static String randomWithSpecialChars(int length) {
        return random(length, ALL_CHARS);
    }

    /**
     * 生成指定字符集的随机字符串
     * @param length 字符串长度
     * @param chars 字符集字符串
     * @return 随机字符串
     */
    public static String random(int length, String chars) {
        if (length <= 0) {
            return "";
        }
        if (chars == null || chars.isEmpty()) {
            throw new IllegalArgumentException("字符集不能为空");
        }

        StringBuilder sb = new StringBuilder(length);
        for (int i = 0; i < length; i++) {
            int index = RANDOM.nextInt(chars.length());
            sb.append(chars.charAt(index));
        }
        return sb.toString();
    }

    /**
     * 生成UUID字符串（无连字符）
     * @return UUID字符串
     */
    public static String randomUUID() {
        return java.util.UUID.randomUUID().toString().replace("-", "");
    }

    /**
     * 生成指定范围的随机字符串（长度随机）
     * @param minLength 最小长度
     * @param maxLength 最大长度
     * @param chars 字符集
     * @return 随机字符串
     */
    public static String random(int minLength, int maxLength, String chars) {
        if (minLength < 0 || maxLength < minLength) {
            throw new IllegalArgumentException("长度范围无效");
        }
        int length = minLength + RANDOM.nextInt(maxLength - minLength + 1);
        return random(length, chars);
    }

    /**
     * 生成指定长度的随机ASCII字符串(可打印字符)
     * @param length 字符串长度
     * @return 随机ASCII字符串
     */
    public static String randomAscii(int length) {
        StringBuilder sb = new StringBuilder(length);
        for (int i = 0; i < length; i++) {
            sb.append((char) (33 + RANDOM.nextInt(94))); // 33-126是可打印ASCII字符
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        // 生成10位字母字符串
        System.out.println("字母字符串: " + RandomStringUtils.randomAlphabetic(10));

        // 生成15位字母数字字符串
        System.out.println("字母数字字符串: " + RandomStringUtils.randomAlphanumeric(15));

        // 生成8位数字字符串
        System.out.println("数字字符串: " + RandomStringUtils.randomNumeric(8));

        // 生成12位包含特殊字符的字符串
        System.out.println("特殊字符字符串: " + RandomStringUtils.randomWithSpecialChars(12));

        // 生成UUID
        System.out.println("UUID: " + RandomStringUtils.randomUUID());

        // 生成长度5-10的随机字母字符串
        System.out.println("可变长度字符串: " + RandomStringUtils.random(5, 10, RandomStringUtils.ALPHA));

        // 生成ASCII字符串
        System.out.println("ASCII字符串: " + RandomStringUtils.randomAscii(10));
    }
}